#include <addressbar.h>

//#include <iostream>

#include <QObject>

AddressBar::AddressBar(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
	QObject::connect(ui.back_button, SIGNAL(clicked()), this, SLOT(clicaram_back()) );
	QObject::connect(ui.next_button, SIGNAL(clicked()), this, SLOT(clicaram_next()) );
	QObject::connect(ui.stop_button, SIGNAL(clicked()), this, SLOT(clicaram_stop()) );
	QObject::connect(ui.reload_button, SIGNAL(clicked()), this, SLOT(clicaram_reload()) );
	QObject::connect(ui.go_button, SIGNAL(clicked()), this, SLOT(clicaram_load()) );
	QObject::connect(ui.line_edit, SIGNAL(returnPressed()), this, SLOT(clicaram_load()) );

}

/** Seta endereço(valor text) do QLineEdit, e emite sinal addressChanged */
void AddressBar::setAddress(const QString &text)
{
    Q_UNUSED(text);
    ui.line_edit->setText(text);
}

void AddressBar::setAddress(const QUrl &url)
{
    Q_UNUSED(url);
    ui.line_edit->setText(url.toString());
}

void AddressBar::clicaram_back()
{
	emit back();
}

void AddressBar::clicaram_next()
{
	emit forward();
}

void AddressBar::clicaram_stop()
{
	emit stop();
}

void AddressBar::clicaram_reload()
{
	emit reload();
}

void AddressBar::clicaram_load()
{
	QString s =ui.line_edit->text();


	if ( !s.startsWith( "http://" ) )
	{
		//std::cout << s.toStdString() << std::endl;
		s.prepend( "http://" );
		ui.line_edit->setText(s);
		//std::cout << s.toStdString() << std::endl;
	}

	QUrl *url = new QUrl(s);
	emit load(*url);
}
